//
//  APPRefreshTonken.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/7/29.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class APPRefreshTonken: NSObject,AlertDelegate {
    static let `default` = APPRefreshTonken()
    var isRequestIngBool:Bool = false
    
    private lazy var refreshToken: RefreshTokenRequest = {
        let token = RefreshTokenRequest()
        token.dataReformer = self
        token.paramSource = self
        token.delegate = self
        return token
    }()
    
    
    func compareTimeLimit() {
        if AppInfo.default.isLogin {
            isRequestIngBool = true
            let now = Date()
            let timeInterval:TimeInterval = now.timeIntervalSince1970
            let timeDate = Int64(timeInterval) * 1000
            if AppInfo.default.expirationDate == nil {
                return
            }
            if !isRequestIngBool  {
                return
            }
            if AppInfo.default.expirationDate! - timeDate <= 60*15 && AppInfo.default.expirationDate! - timeDate > 0{
                self.refreshToken.loadData([:])
            }else if AppInfo.default.expirationDate! - timeDate <= 0 {
                AppInfo.default.isLogin = false
                if CYMBShow.currentController() != nil {
                    Alert.default.delegate = self
                    Alert.default.show(title: kText("登录已失效,请退出重新登录"), message:"", actions: [kText("取消"),kText("重新登录")], from: CYMBShow.currentController()!) {
                        
                    }
                    return
                }else {
                    CYMBShow.showMessage(kText("登录已失效,请退出重新登录"))
                    return
                }
            }
        }
    }
}
extension APPRefreshTonken: RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        isRequestIngBool = false
        if data == nil {
            return
        }
        if  (data!["timestamp"] as? Int64) != nil {
            AppInfo.default.expirationDate = data?["timestamp"] as? Int64
        }
        guard let dic = data!["data"] as? [String : Any] else {
            return
        }
        if dic["token"] as? String != nil {
            AppInfo.default.token = dic["token"] as? String
        }
        AppInfo.default.write()
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        isRequestIngBool = false
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        let pramas: [String:Any] = [:]
        
        return pramas
    }
    
    func alert(didSelected title: String?) {
        if title == kText("重新登录") {
            let controller:UIViewController =  CYMBShow.currentController()!
            let pop = LoginViewController()
            pop.isNeedPop = true
            controller.navigationController?.pushViewController(pop, animated: true)
        }else {
            let controller:UIViewController =  CYMBShow.currentController()!
            controller.navigationController?.popToRootViewController(animated: true)
        }
        
    }
}

